$package('YiYa.merchant');
YiYa.merchant = function(){
	var _box = null;
	var _this = {
			editIndex : undefined,
			importExcelAction:'importExcel.do',
			importExcelForm:function(){
				return $("#excelForm");
			},
			importExcelWin:function(){
				return $("#import-excel-win");
			},
			saveExcel:function(){
				YiYa.progress();//缓冲条
				_this.importExcelForm().attr('action',_this.importExcelAction);
				$('#excelForm').attr('enctype', 'multipart/form-data');
				$('#excelForm').ajaxSubmit(function(data){
					var result = jQuery.parseJSON(data)
					if(result.success){
						YiYa.closeProgress();//关闭缓冲条
						_this.importExcelWin().dialog('close');
						_box.handler.refresh();
					}else{
						YiYa.alert('提示', result.msg || "请求出现异常,请联系管理员", 'error');
						YiYa.closeProgress();//关闭缓冲条
					}
				});
			},
			initForm:function(){
				//修改密码
				_this.importExcelWin().find("#btn-excel-submit").click(function(){
					_this.saveExcel();
				});
				_this.importExcelWin().find("#btn-excel-close").click(function(){	
					$.messager.confirm('确认','确定要关闭窗口吗?',function(r){  
					    if (r){  
					     	_this.importExcelWin().dialog('close');
					    }  
					});
				});
			},
			endEditing:function(){
	            if (_this.editIndex == undefined){return true}
	            if ($('#data-list').datagrid('validateRow', _this.editIndex)){
	                var ed = $('#data-list').datagrid('getEditor', {index:_this.editIndex,field:'payment_date'});
	                var payment_date = $(ed.target).datebox('getText');
	                $('#data-list').datagrid('getRows')[_this.editIndex]['paymentDate'] = payment_date;
	                
	                var ed1 = $('#data-list').datagrid('getEditor', {index:_this.editIndex,field:'merchantId'});
	                var merchantName = $(ed1.target).combobox('getText')
	                $('#data-list').datagrid('getRows')[_this.editIndex]['merchantName'] = merchantName;
	                
	                var ed2 = $('#data-list').datagrid('getEditor', {index:_this.editIndex,field:'tax'});
	                var taxDisplay = $(ed2.target).combobox('getText')
	                $('#data-list').datagrid('getRows')[_this.editIndex]['taxDisplay'] = taxDisplay;
	                
	                var ed3 = $('#data-list').datagrid('getEditor', {index:_this.editIndex,field:'payStatus'});
	                var payStatusDisplay = $(ed3.target).combobox('getText')
	                $('#data-list').datagrid('getRows')[_this.editIndex]['payStatusDisplay'] = payStatusDisplay;
	                
	                $('#data-list').datagrid('endEdit', _this.editIndex);
	                _this.editIndex = undefined;
	                return true;
	            } else {
	                return false;
	            }
	        },
	        append:function(){
	            if (_this.endEditing()){
	                $('#data-list').datagrid('appendRow',{id:''});
	                _this.editIndex = $('#data-list').datagrid('getRows').length-1;
	                $('#data-list').datagrid('selectRow', _this.editIndex)
	                        .datagrid('beginEdit', _this.editIndex);
	            }
	        },
	        removeit:function(){
	            if (_this.editIndex == undefined){return}
	            $('#data-list').datagrid('cancelEdit', _this.editIndex)
	                    .datagrid('deleteRow', _this.editIndex);
	            _this.editIndex = undefined;
	        },
	        reject:function(){
	            $('#data-list').datagrid('rejectChanges');
	            editIndex = undefined;
	        },
	        accept:function(){
	            if (_this.endEditing()){
	                //$('#data-list').datagrid('acceptChanges');
	                var inserted = $('#data-list').datagrid('getChanges', "inserted"); 
	                var deleted = $('#data-list').datagrid('getChanges', "deleted");  
                    var updated = $('#data-list').datagrid('getChanges', "updated");
                    var effectRow = new Object();  
                    if (inserted.length) {  
                        effectRow["inserted"] = JSON.stringify(inserted);  
                    }  
                    if (deleted.length) {  
                        effectRow["deleted"] = JSON.stringify(deleted);  
                    }  
                    if (updated.length) {  
                        effectRow["updated"] = JSON.stringify(updated);  
                    } 
                    $.post("commit.do",effectRow,function(data,status){
                    	if(data.success){
                    		var rows = $('#data-list').datagrid('getChanges');
                    		alert(data.msg+",共更改"+rows.length+"条记录");
                    	}else{
                    		alert(data.msg);
                    	}
                    	$('#data-list').datagrid('acceptChanges');
                    });
	            }
	        },
		config:{
			dataGrid:{
				title:'请款列表',
				url:'dataList.do',
				onClickRow:function(index){
					//alert(index);
		            if (_this.editIndex != index){
		                if (_this.endEditing()){
		                    $('#data-list').datagrid('selectRow', index)
		                            .datagrid('beginEdit', index);
		                    _this.editIndex = index;
		                    var ed = $('#data-list').datagrid('getEditor', {index:_this.editIndex,field:'payment_date'});
		                    $(ed.target).datebox('setValue',  $('#data-list').datagrid('getRows')[_this.editIndex]['paymentDate']);
		                } else {
		                    $('#data-list').datagrid('selectRow', _this.editIndex);
		                }
		            }
		       },
				columns:[[
				          {field:'id',checkbox:true},
				          {field:'merchantId',title:'客户名称',width:120,
				        	  formatter:function(value,row){
		                            return row.merchantName;
		                        },
				        	  editor:{type:'combobox',options:{
				        		  valueField:'id',
				        		  textField:'name',
				        		  method:'get',
				        		  url:'../merchant/loadMerchantList.do?type=2',
				        		  required:'true'
				        	  }}
				        	  },
				          {field:'month',title:'月份',width:120,editor:{type:'numberbox',options:{min:0,max:12}}},
				          {field:'paymentTime',title:'月结时间',width:120,editor:{type:'numberbox',options:{min:0}}},
				          {field:'discount',title:'是否扣%',width:120,editor:{type:'numberbox',options:{precision:2}}},
				          {field:'tax',title:'是否含税',width:120,
				        	  formatter:function(value,row){
		                            return row.taxDisplay;
		                      },
				        	  editor:{type:'combobox',
				        	  options:{
				        		  valueField:'id',
				        		  textField:'value',
				        		  data:[{
				        			  id:'0',
				        			  value:'否'
				        		  },{
				        			  id:'1',
				        			  value:'是'
				        		  }]
				        	  }
				        	  }},
				          {field:'amount',title:'月结金额',width:120,sortable:true,editor:{type:'numberbox',options:{precision:2}}},
				          {field:'invoiceAmount',title:'开票金额',width:120,sortable:true,editor:{type:'numberbox',options:{precision:2}}},
				          {field:'payAmount',title:'付款金额',width:120,sortable:true,editor:{type:'numberbox',options:{precision:2}}},
				          {field:'payment_date',title:'付款日期',width:120,sortable:true,
				        	  formatter:function(value,row,index){
				        		  if(row["paymentDate"]!=null){
				        		  return new Date(row["paymentDate"].split(" ")[0].replace(/-/g,'/')).format("yyyy-MM-dd");
				        		  }
						      },editor:{type:'datebox',required:true}
						  },
						  {field:'payStatus',title:'是否已付款',width:120,
							  formatter:function(value,row){
	                            return row.payStatusDisplay;
							  },
							  editor:{type:'combobox',
				        	  options:{
				        		  valueField:'id',
				        		  textField:'value',
				        		  data:[{
				        			  id:'0',
				        			  value:'否'
				        		  },{
				        			  id:'1',
				        			  value:'是'
				        		  }]
				        	  }
				        	  }},
				          {field:'remark',title:'备注',width:120,
							  formatter:function(value,row,index){
								  if(value!=undefined){
									  if(value.length>10){
										  var str = value.substring(0,8);
										  return str.concat('......');	  
									  }
									  return value;
								  }
								  
							  },editor:'text'}
				          ]],
				          toolbar:[
				                   /*
				                   {id:'btnadd',text:'添加',btnType:'add'},
				                   {id:'btnedit',text:'编辑',btnType:'edit'},
				                   */
								   {id:'btnappend',text:'添加',btnType:'append',iconCls:'icon-add',handler:function(){
									   _this.append();
								   }},
								   /*
								    
								   {id:'btnremove',text:'删除',btnType:'remove',iconCls:'icon-remove',handler:function(){
									   _this.removeit();
								   }}
								   */
								   {id:'btndelete',text:'删除',btnType:'remove'},
								   {id:'btnreject',text:'放弃更改',btnType:'reject',iconCls:'icon-undo',handler:function(){
									   _this.reject();
								   }},
								   {id:'btnaccept',text:'保存更改',btnType:'accept',iconCls:'icon-save',handler:function(){
									   _this.accept();
								   }},
								   /*
								   {id:'btnedit',text:'导入Excel',btnType:'importExcel',iconCls:'icon-edit',handler:function(){
										_this.importExcelForm().resetForm();
										_this.importExcelWin().window('open'); 
							       }},
							       {id:'btnview',text:'查看',btnType:'view'}
							       */
				                   ]
			}
		},
		init:function(){
			_this.initForm();
			_box = new YDataGrid(_this.config);
			_box.init();
		}
	}
	return _this;
}();

$(function(){
	YiYa.merchant.init();
});


